<template>
  <div style="padding: 15px;">
    <vxe-toolbar id="warehouseLocationId" :custom="{storage: true}" export :refresh="{query: findList}">
      <template v-slot:buttons>
        <el-button @click="findList">查询</el-button>
      </template>
    </vxe-toolbar>
    <vxe-table
      ref="xTable"
      border
      resizable
      stripe
      highlight-current-row
      :export-config='{type:"csv"}'
      :align="allAlign"
      show-footer
      highlight-hover-row
      row-id="id"
      class="mytable-footer"
      height="700"
      :loading="loading"
      :data="tableData"
      :edit-config="{trigger: 'click', mode: 'row', showStatus: true}"
      @sort-change="sortList"
      auto-resize
      :seq-config="{startIndex: listQuery.pageNum * listQuery.pageSize - listQuery.pageSize}"
    >
      <vxe-table-column type="seq" title="序号" width="60"></vxe-table-column>
      <vxe-table-column title="任务标识" field="code" min-width="300">
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="dataType" title="数据类型" min-width="140" sortable show-header-overflow show-overflow />
        <template #header>
          <el-select v-model="listQuery.dataType" class="w100" clearable filterable placeholder="请选择">
            <el-option
              v-for="item in typeList"
              :key="item.code"
              :label="item.name"
              :value="item.code"
            />
          </el-select>
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="status" title="状态" min-width="140" sortable show-header-overflow show-overflow>
          <template v-slot="{ row }">
            <i v-if="row.status === '处理中'" class="el-icon-loading"></i>
            <span>{{ `${ row.status }` }}</span>
          </template>
        </vxe-table-column>
        <template #header>
          <el-select v-model="listQuery.status" class="w100" clearable filterable placeholder="请选择">
            <el-option
              v-for="item in statusList"
              :key="item.code"
              :label="item.name"
              :value="item.code"
            />
          </el-select>
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="gmtCreated"
          title="创建日期"
          min-width="280"
          sortable
          show-header-overflow
          show-overflow
        />
        <template #header>
          <el-date-picker
            v-model="gmtCreated"
            type="daterange"
            value-format="yyyy-MM-dd"
            style="width:100%"
            :picker-options="pickerOptions"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            @change="(value) => changeDate(value, 'gmtCreated')"
          />
        </template>
      </vxe-table-column>
      <vxe-table-column title="操作" fixed="right" min-width="60">
        <template slot-scope="scope" v-if="scope.row.status === '完成'">
          <vxe-button type="text" @click="funcDerive(scope.row.downloadLink)">
            <el-link :underline="false" :href="linkDerive" type="primary">
              下载
            </el-link>
          </vxe-button>
        </template>
      </vxe-table-column>
    </vxe-table>
    <vxe-pager
      :loading="loading"
      :current-page.sync="listQuery.pageNum"
      :page-size.sync="listQuery.pageSize"
      :total="listQuery.total"
      :layouts="['PrevJump', 'PrevPage', 'Number', 'NextPage', 'NextJump', 'Sizes', 'FullJump', 'Total']"
      @page-change="handlePageChange"
    />
  </div>
</template>

<script>
import Vue from 'vue'
import 'xe-utils'
import VXETable from 'vxe-table'
import 'vxe-table/lib/index.css'
import XEUtils from 'xe-utils'
import VXEUtils from 'vxe-utils'
Vue.use(VXETable, VXEUtils, XEUtils, { mounts: ['cookie'] })
import { exportTaskList  } from '@/api/basicInfo'
export default {
  name: '导出任务',
  data() {
    return {
      gmtCreated:'',
      // 日期选项
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
            picker.$emit('pick', [start, end]);
          }
        }]
      },
      loading: false,
      allAlign: 'center',
      tableData: [],
      statusList:[
        { code: "IN_PROCESS", name: "处理中" },
        { code: "COMPLETED",name: "完成" },
        { code: "FAIL",name: "失败" },
      ],
      linkDerive:'',
      // 类型
      typeList: [
        // { code: "RECEIVE", name: "收货单"},
        // { code: "INBOUND", name: "入库单"},
        // { code: "OUTBOUND", name: "出库单"},
        // { code: "PICKING", name: "拣货单"},
        { code: "INVENTORS", name: "实时库存"},
        { code: "INBOUNDS", name: "入库单"},
        { code: "DELIVERED", name: "订单详情"},
        { code: "DELIVERY", name: "订单"},
        { code: "INVENTORY", name: "库存"}
      ],
      listQuery: {
        pageNum: 1,
        pageSize: 15
      },
    }
  },
  created() {
    this.findList()
    // 设置自动刷新间隔，单位毫秒，这里给设置成1分钟
    this.timer = setInterval(()=> this.findList(), 1000 * 60 * 1)
  },
  destroyed() {
    clearInterval(this.timer)
  },
  methods: {
    //获取列表
    findList(noLoading) {
      if (!noLoading) {
        this.loading = true
      }
      return exportTaskList(this.listQuery).then(response => {
        this.tableData = response.data.records
        this.listQuery.total = response.data.total
      }).finally(() => {
        this.loading = false
      })
    },
    // 排序
    sortList({ column, property, order }) {
      if (order) {
        this.listQuery.sortField = property
        this.listQuery.sortType = order
      } else {
        this.listQuery.sortField = ''
      }
      this.findList()
    },
    funcDerive(link) {
      this.linkDerive = link
    },
    // 时间
    changeDate(value, prefix) {
      if (value) {
        this.listQuery[`${prefix}Start`] = value[0] + " 00:00:00";
        this.listQuery[`${prefix}End`] = value[1] + " 23:59:59";
      } else {
        this.listQuery[`${prefix}Start`] = "";
        this.listQuery[`${prefix}End`] = "";
      }
    },
    // 分页handle
    handlePageChange({ currentPage, pageSize }) {
      this.listQuery.pageNum = currentPage
      this.listQuery.pageSize = pageSize
      this.findList()
    }
  }
}
</script>

<style scoped>
.vxe-table .vxe-body--column:not(.col--ellipsis), .vxe-table .vxe-footer--column:not(.col--ellipsis), .vxe-table .vxe-header--column:not(.col--ellipsis) {
 padding: 6px 0;
}
.vxe-toolbar{padding:0 0 10px 0}
.w100 {
  width: 100%;
}
</style>
